//
//  AuthTool.swift
//  Nairacup
//
//  Created by ec on 2024/9/19.
//

import Foundation
import Contacts
import CoreLocation
import Photos
import EventKit

enum AuthStatus {
    case notApply
    case authorized
    case denied
}

struct AuthTool {
    
    static func checkContactsPermission(_ complate: (AuthStatus)->Void) {
        let authorizationStatus = CNContactStore.authorizationStatus(for: .contacts)
        switch authorizationStatus {
        case .notDetermined:
            print("尚未决定，申请权限")
            complate(.notApply)
        case .restricted, .denied:
            print("受限或被拒绝")
            complate(.denied)
        case .authorized:
            print("已授权")
            complate(.authorized)
        @unknown default:
            print("未知权限状态")
            complate(.denied)
        }
    }
    
    static func checkLocationPermission(_ complate: (AuthStatus)->Void) {
        let authorizationStatus = CLLocationManager.authorizationStatus()
        switch authorizationStatus {
        case .notDetermined:
            print("尚未决定")
            complate(.notApply)
        case .restricted, .denied:
            print("受限或被拒绝")
            complate(.denied)
        case .authorizedWhenInUse, .authorizedAlways:
            print("已授权")
            complate(.authorized)
        @unknown default:
            print("未知权限状态")
            complate(.denied)
        }
    }
    
    static func checkPhotoLibraryPermission(_ complate: (AuthStatus)->Void) {
        let status = PHPhotoLibrary.authorizationStatus()
        switch status {
        case .notDetermined:
            print("尚未决定")
            complate(.notApply)
        case .restricted, .denied:
            print("被拒绝或受限")
            complate(.denied)
        case .authorized, .limited:
            print("已授权或已授予有限的相册权限")
            complate(.authorized)
        @unknown default:
            print("未知的权限状态")
            complate(.denied)
        }
    }

    static func checkCameraPermission(_ complate: (AuthStatus)->Void) {
        let cameraAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)
        switch cameraAuthorizationStatus {
        case .notDetermined:
            print("尚未决定，申请权限")
            complate(.notApply)
        case .restricted, .denied:
            print("受限或被拒绝")
            complate(.denied)
        case .authorized:
            print("已授权")
            complate(.authorized)
        @unknown default:
            print("未知权限状态")
            complate(.denied)
        }
    }
    
    // 请求访问权限
    static func checkCalendarPermission(_ complate: (AuthStatus)->Void) {
        let status = EKEventStore.authorizationStatus(for: .event)
        switch status {
        case .notDetermined:
            // 用户还没有作出选择，应该请求权限
            complate(.notApply)
        case .restricted:
            // 用户的设备限制了访问
            complate(.denied)
        case .denied:
            // 用户拒绝了访问日历
            complate(.denied)
            // 提醒用户去设置中启用权限
        case .authorized:
            // 用户已授权
            complate(.authorized)
        default:
            complate(.denied)
        }
    }

    static func applyContactsAuth(_ complate: @escaping (AuthStatus)->Void) {
        let contactStore = CNContactStore()
        contactStore.requestAccess(for: .contacts) { granted, error in
            Async.main {
                complate(granted ? .authorized : .denied)
            }
        }
    }
    
    static func applyPhotoAuth(_ complate: @escaping (AuthStatus)->Void) {
        PHPhotoLibrary.requestAuthorization { status in
            switch status {
            case .authorized:
                print("相册权限已授权")
                Async.main {
                    complate(.authorized)
                }
            case .denied, .restricted:
                print("相册权限被拒绝或受限")
                Async.main {
                    complate(.denied)
                }
            case .limited:
                print("用户已授予有限的相册权限")
                Async.main {
                    complate(.authorized)
                }
            default:
                Async.main {
                    complate(.denied)
                }
                break
            }
        }

    }
    
    static func applyCameraAuth(_ complate: @escaping (AuthStatus)->Void) {
        AVCaptureDevice.requestAccess(for: .video) { granted in
            Async.main {
                complate(granted ? .authorized : .denied)
            }
        }
    }
    
    static func applyCalendarAuth(_ complate: @escaping (AuthStatus)->Void) {
        let eventStore = EKEventStore()
        eventStore.requestAccess(to: .event) { granted, error in
            complate(granted ? .authorized : .denied)
        }
    }

}
